package com.study.spring.netty.time_client;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

public class TimeClientEncode extends MessageToByteEncoder<Request> {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    protected void encode(ChannelHandlerContext ctx, Request msg, ByteBuf out) throws Exception {
        logger.info("question is:{}", msg);
        out.writeInt(0xAA);
        out.writeInt(msg.getLength());
        out.writeBytes(msg.getQuestion().getBytes(StandardCharsets.UTF_8));
        out.writeInt(0xAD);
        ctx.flush();
    }
}
